package otherapi.itext.kernellayoutpdf;

import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.WriterProperties;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.DashedBorder;
import com.itextpdf.layout.borders.DottedBorder;
import com.itextpdf.layout.borders.DoubleBorder;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.IBlockElement;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.text.DocumentException;
import org.junit.Test;
import otherapi.itext.PdfTest;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 9/26/2022 9:34 AM
 */
public class ItextKernelLayoutTest {

    private static final String FILENAME = "test.pdf";

    @Test
    public void test1() throws Exception {
        String fileName = "test1";
        String oldPath = fileName + ".pdf";
        OutputStream out = Files.newOutputStream(Paths.get(oldPath));
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(byteArrayOutputStream, new WriterProperties().setFullCompressionMode(true)));
        // 创建默认文档格式
        Document document = new Document(pdfDoc, com.itextpdf.kernel.geom.PageSize.A4);
        float[] widthArr = new float[]{0.5f, 0.5f};

        List<IBlockElement> blockElementList = new ArrayList<>();
        Table table = new Table(widthArr).useAllAvailableWidth();
        table.setBorder(new DoubleBorder(1));
        table.setWidth(200);
        table.setHeight(500);
        table.setPadding(10);
        table.setBorder(new DottedBorder(1));

        Cell cell = new Cell();
        cell.setHeight(100);
        cell.setWidth(50);

        // Table table1 = new Table(widthArr);
        // table1.addCell("helloworlddasfasdczghfdhpipytopwkmgkd\nsfgmlsfvvcxxzvaaaaaaaaacccccc");
        // table1.setHeight(50);
        // table1.setWidth(50);
        // table1.setPadding(5);
        // table1.setHorizontalAlignment(HorizontalAlignment.CENTER);

        // cell.add(table1);
        cell.setBorder(new DashedBorder(1));


        table.addCell(cell);
        table.addCell("test");
        blockElementList.add(table);

        for (int i = 0; i < blockElementList.size(); i++) {
            document.add(blockElementList.get(i));
        }
        document.close();
        byteArrayOutputStream.writeTo(out);
    }

    @Test
    public void test2() throws IOException, DocumentException {
        String fileName = "test1";
        String oldPath = fileName + ".pdf";
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        OutputStream out = new FileOutputStream(oldPath);
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(byteArrayOutputStream, new WriterProperties().setFullCompressionMode(true)));
        Document document = new Document(pdfDoc, com.itextpdf.kernel.geom.PageSize.A4);
        // 创建默认文档格式
        float[] widthArr = {0.5f, 0.5f};
        List<IBlockElement> blockElementList = new ArrayList<>();
        Table table = new Table(widthArr).useAllAvailableWidth();
        table.setBorder(new DoubleBorder(1));
        table.setWidth(200);
        table.setHeight(500);
        table.setPadding(10);
        table.setBorder(new DottedBorder(1));
        Cell cell = new Cell();
        cell.setHeight(100);
        cell.setWidth(50);
        cell.setBorder(new DashedBorder(1));
        // table.addCell(cell);
        // 表格居中
        // table.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 文字居中
        // table.setTextAlignment(TextAlignment.CENTER);
        table.addCell("test");
        blockElementList.add(table);
        int size = blockElementList.size();
        for (int i = 0; i < size; i++) {
            document.add(blockElementList.get(i));
        }
        document.close();
        byteArrayOutputStream.writeTo(out);
    }

    /**
     * 三个表格 表格设置 margin 有表格被覆盖
     *
     * @throws IOException
     * @throws DocumentException
     */
    @Test
    public void test3() throws IOException, DocumentException {
        String fileName = PdfTest.TEST_1_PDF;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        OutputStream out = new FileOutputStream(fileName);
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(byteArrayOutputStream, new WriterProperties().setFullCompressionMode(true)));
        Document document = new Document(pdfDoc, PageSize.A4);
        // 创建默认文档格式
        float[] widthArr = {0.5f, 0.5f};
        float[] widthArr1 = {0.5f};
        List<IBlockElement> blockElementList = new ArrayList<>();

        Table table = new Table(widthArr).useAllAvailableWidth();
        Table table1 = new Table(widthArr).useAllAvailableWidth();
        Table table2 = new Table(widthArr1).useAllAvailableWidth();

        table.setWidth(200);
        table.setHeight(200);
        table.setBorder(new SolidBorder(1));

        table1.setWidth(200);
        table1.setHeight(200);
        table1.setBorder(new SolidBorder(1));
        // table1.setMargins(150, 0, 0, 0);

        table2.setWidth(200);
        table2.setHeight(240);
        table2.setBorder(new SolidBorder(1));
        for (int i = 0; i < 10; i++) {
            Cell cell = new Cell();
            cell.setHeight(20);
            cell.setWidth(80);
            // cell.setBorder(new DashedBorder(1));
            cell.add(new Paragraph("abc" + i));
            table.setPadding(20);
            table.addCell(cell);
            table1.addCell(cell);
        }
        table.setPadding(5);
        Cell cell = new Cell();
        cell.add(table1);
        table2.setMarginTop(100);
        table2.addCell(cell);
        blockElementList.add(table2);
        blockElementList.add(table);
        int size = blockElementList.size();
        for (int i = 0; i < size; i++) {
            document.add(blockElementList.get(i));
        }
        document.close();
        byteArrayOutputStream.writeTo(out);
    }

}
